///**
// * Created by zhangxiaoyu on 2016-09-18.
// */
angular.module('hyt.mailListService', [])

    //通讯录
    .factory('MailShrFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result;
      var mailShrData={},
          pagesize=10;
      return{
        getMailShr:function(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#querykhxx',
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'sflx':1,
              'khmc':khmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }

              mailShrData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('MailShr.shrUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getShr : function(){
          if(mailShrData[pagesize] == undefined){
            return false;
          }
          return mailShrData[pagesize];
        },
        getMoreShr:function(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(mailShrData[pagesize] == undefined){
            return false;
          }

          //获取以前的数据
          var hasNextPage = mailShrData[pagesize].hasNextPage;
          var current_page = mailShrData[pagesize].current_page;
          var moreShrListData = mailShrData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#querykhxx',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'sflx':1,
              'khmc':khmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreShrListData = moreShrListData.concat(r.ACTION_INFO.RESULT_LIST);

              mailShrData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreShrListData
              };
              $rootScope.$broadcast('MailShr.shrUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (mailShrData[pagesize] === undefined) {
            return false;
          }
          return mailShrData[pagesize].hasNextPage;
        },

        getDelete:function(sid,user_id,dw_id,yh_id,kh_id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'beanBiz.delete#Ykhxx',
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'khId':kh_id
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000") {
              result = r.ACTION_INFO.result;
              $rootScope.$broadcast('DeleteData');
            }
          });
        },
        deleteResult:function(){
          return result;
        }
      }
    })

    .factory('MailFhrFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result;
      var mailFhrData={},
          pagesize=10;
      return{
        getMailFhr:function(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#querykhxx',
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'sflx':2,
              'khmc':khmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }

              mailFhrData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('MailFhr.fhrUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getFhr : function(){
          if(mailFhrData[pagesize] == undefined){
            return false;
          }
          return mailFhrData[pagesize];
        },
        getMoreFhr:function(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(mailFhrData[pagesize] == undefined){
            return false;
          }

          //获取以前的数据
          var hasNextPage = mailFhrData[pagesize].hasNextPage;
          var current_page = mailFhrData[pagesize].current_page;
          var moreFhrListData = mailFhrData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#querykhxx',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'sflx':2,
              'khmc':khmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreFhrListData = moreFhrListData.concat(r.ACTION_INFO.RESULT_LIST);

              mailFhrData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreFhrListData
              };
              $rootScope.$broadcast('MailFhr.fhrUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (mailFhrData[pagesize] === undefined) {
            return false;
          }
          return mailFhrData[pagesize].hasNextPage;
        }
      }
    })

    .factory('MailSjFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result;
      var mailSjData={},
          pagesize=10;
      return{
        getMailSj:function(sid,user_id,yh_id,dw_id,yhmc,lxdh,pyjm){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#querysj',
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'yhmc':yhmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }

              mailSjData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('MailSj.sjUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getSj : function(){
          if(mailSjData[pagesize] == undefined){
            return false;
          }
          return mailSjData[pagesize];
        },
        getMoreSj:function(sid,user_id,yh_id,dw_id,yhmc,lxdh,pyjm){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(mailSjData[pagesize] == undefined){
            return false;
          }

          //获取以前的数据
          var hasNextPage = mailSjData[pagesize].hasNextPage;
          var current_page = mailSjData[pagesize].current_page;
          var moreSjListData = mailSjData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#querysj',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':dw_id,
              'yhmc':yhmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreSjListData = moreSjListData.concat(r.ACTION_INFO.RESULT_LIST);

              mailSjData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreSjListData
              };
              $rootScope.$broadcast('MailSj.sjUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (mailSjData[pagesize] === undefined) {
            return false;
          }
          return mailSjData[pagesize].hasNextPage;
        }
      }
    });